//
//  StoreSetionView.m
//  FKGW
//
//  Created by song ce on 2017/2/21.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "StoreSetionView.h"
#import "UIView+Extend.h"
#import "AppDelegate.h"

#define ImageWith  8

@interface StoreSetionView ()
@property(nonatomic,strong)UILabel *cLabel;
@property(nonatomic,strong)UIImageView *cImgeV;
@end

@implementation StoreSetionView

- (void)awakeFromNib {
    self.contentView.backgroundColor = [UIColor whiteColor];
    [super awakeFromNib];
    self.titles = [NSMutableArray array];
    self.cLabel.text = @"分类";
    CGFloat with = [self getContentWith:@"分类"];
    self.cLabel.left = (self.sortView.width-with-5-ImageWith)/2;
    self.cLabel.width = with;
    self.cImgeV.image = [UIImage imageNamed:@"icon_hangyexiala_nor"];
  }

-(void)setTypeArray:(NSArray *)typeArray{
    _typeArray = typeArray;
    for(int i=0;i<_typeArray.count;i++){
        ProductModel *model = _typeArray[i];
        [self.titles addObject:model.name];
    }
}

-(void)setProductName:(NSString *)productName{
    _productName= productName;
    if(_productName.length){
        self.cLabel.text = _productName;
        CGFloat with = [self getContentWith:_productName];
        self.cLabel.left = (self.sortView.width-with-5-ImageWith)/2;
        self.cLabel.width = with;
        self.cImgeV.left = self.cLabel.right+3;
//         [self.sortButton setTitle:_productName forState:UIControlStateNormal];
//        CGRect frame = [_productName boundingRectWithSize:CGSizeMake(1000, 22) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
//        CGFloat with = CGRectGetWidth(frame);
//        CGFloat with =50;
//        if(_productName.length/2.0==1){
//            with = 50;
//        }
//        if(_productName.length/2.0==1.5){
//            with=  _productName.length/2.0*36;
//        }
//        if(_productName.length/2.0==2){
//          with=  _productName.length/2.0*33;
//        }
//        if(_productName.length/2.0==2.5){
//            with=  _productName.length/2.0*28;
//        }

//        [self.sortButton setImageEdgeInsets:UIEdgeInsetsMake(0, with, 0, 0)];
    }else{
//        [self.sortButton setTitle:@"分类" forState:UIControlStateNormal];
//         [self.sortButton setImageEdgeInsets:UIEdgeInsetsMake(0, 51, 0, 0)];
        self.cLabel.text = @"分类";
        CGFloat with = [self getContentWith:@"分类"];
        self.cLabel.left = (self.sortView.width-with-5-ImageWith)/2;
        self.cLabel.width = with;
        self.cImgeV.image = [UIImage imageNamed:@"icon_hangyexiala_nor"];
        self.cImgeV.left = self.cLabel.right+3;
    }
}

-(void)setSalesSortType:(NSString *)salesSortType{
    _salesSortType = salesSortType;
    [self settingBtn:self.xiaoliangBtn count:[_salesSortType integerValue]];
}

-(void)setPriceSortType:(NSString *)priceSortType{
    _priceSortType = priceSortType;
       [self settingBtn:self.priceBtn count:[_priceSortType integerValue]];
}

//选择分类
- (IBAction)sort:(UIButton *)sender {
    if(self.sortBlock){
        self.sortBlock(sender,self.cImgeV,self.cLabel);
    }
}
//按销量排序
- (IBAction)xiaoliang:(UIButton *)sender {
    if(self.sortButton.isSelected){
        [self sort:self.sortButton];
        return;
    }
    [AppDelegate appDealegate].xiaoliangClickCount++;
     [self settingBtn:sender count:  [AppDelegate appDealegate].xiaoliangClickCount];
    
    if(self.saleCountBlock){
        self.saleCountBlock([AppDelegate appDealegate].xiaoliangClickCount%3);
    }
}
//按价格排序
- (IBAction)price:(UIButton *)sender {
    if(self.sortButton.isSelected){
        [self sort:self.sortButton];
        return;
    }
        [AppDelegate appDealegate].priceClickCount++;
//    if(self.priceClickCount%3==1){
//        [sender setImage:[UIImage imageNamed:@"congdidaogao"] forState:UIControlStateNormal];
//    }else if(self.priceClickCount%3==2){
//        [sender setImage:[UIImage imageNamed:@"conggaodaodi"] forState:UIControlStateNormal];
//    }else{
//        [sender setImage:[UIImage imageNamed:@"anfenleipaixu"] forState:UIControlStateNormal];
//    }
    [self settingBtn:sender count:[AppDelegate appDealegate].priceClickCount];
    
    if(self.priceBlock){
        self.priceBlock([AppDelegate appDealegate].priceClickCount%3);
    }
}


-(void)settingBtn:(UIButton*)sender  count:(NSInteger)count{
    if(count%3==1){
        [sender setImage:[UIImage imageNamed:@"congdidaogao"] forState:UIControlStateNormal];
    }else if(count%3==2){
        [sender setImage:[UIImage imageNamed:@"conggaodaodi"] forState:UIControlStateNormal];
    }else{
        [sender setImage:[UIImage imageNamed:@"anfenleipaixu"] forState:UIControlStateNormal];
    }

}
-(UILabel *)cLabel{
    if(_cLabel==nil){
        _cLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 0, 22)];
        _cLabel.textColor = DEF_COLOR102;
        _cLabel.font = [UIFont systemFontOfSize:15];
        [self.sortView addSubview:_cLabel];
    }
    return _cLabel;
}

-(UIImageView *)cImgeV{
    if(_cImgeV==nil){
        _cImgeV = [[UIImageView alloc]initWithFrame:CGRectMake(self.cLabel.right +3, 19, 8, 5)];
        [self.sortView addSubview:_cImgeV];
    }
    return _cImgeV;
}

-(CGFloat)getContentWith:(NSString*)content{
        CGRect frame = [content boundingRectWithSize:CGSizeMake(1000, 22) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
         CGFloat with = CGRectGetWidth(frame);
    return with+2;
}



@end
